package online.foxnull2.foxmall.search.web;

import online.foxnull2.foxmall.search.service.MySearchService;
import online.foxnull2.foxmall.search.vo.SearchParamVo;
import online.foxnull2.foxmall.search.vo.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class SearchController {

    private final MySearchService mySearchService;

    @Autowired
    public SearchController(MySearchService mySearchService) {
        this.mySearchService = mySearchService;
    }

    @GetMapping("/list.html")
    public String search(SearchParamVo param,
                         Model model,
                         HttpServletRequest request) {
        String queryString = request.getQueryString();
        SearchResult result = mySearchService.search(param, queryString);
        model.addAttribute("result", result);

        return "index";
    }
}
